﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PhoneCore.Framework;
using PhoneCore.Framework.Bootstrap;
using PhoneCore.Framework.Configuration;
using PhoneCore.Framework.IoC;
using SecureBox.UI.Infrastructure.Data;

using Template = SecureBox.UI.Infrastructure.Model.Template;
using Record = SecureBox.UI.Infrastructure.Model.Record;
using Property = SecureBox.UI.Infrastructure.Model.Property;
using Keyword = SecureBox.UI.Infrastructure.Model.Keyword;

namespace SecureBox.UI.UnitTests.Infrastructure
{
    [TestClass]
    public class DataTests
    {
        [TestMethod]
        public void CanAddRecord()
        {
            //arrange
            using (IContainer container = TestHelper.GetContainer())
            {
                container.RegisterType(typeof(IBootstrapperService), typeof(BootstrapperService),
                                    ConfigSettings.Instance.GetSection("system/bootstrapping"));
                IBootstrapperService service = container.Resolve<IBootstrapperService>();
                service.Run();

                using (IDataContextService dataContextService = container.Resolve<IDataContextService>())
                {
                    Record rec = new Record();
                    rec.Name = "test name";
                    rec.CreationDate = DateTime.Now;
                    rec.Template = dataContextService.Templates.Get(t => t.Name == "Account");
                    rec.Properties = new List<Property>()
                                         {
                                             new Property() {Name = "Email", Value = "email@test.com"},
                                             new Property() {Name = "Password", Value = "my password"},
                                         };
                    rec.Keywords = new List<Keyword>()
                                       {
                                           new Keyword() {Name = "test"}
                                       };

                    //act
                    dataContextService.Records.Add(rec);

                    //Assert
                    var record = dataContextService.Records.Get(r => r.Name == "test name");
                    Assert.AreEqual("Account", record.Template.Name);
                    Assert.AreEqual("test name", record.Name);
                    Assert.AreEqual("email@test.com", record.Properties.Single(p => p.Name == "Email").Value);
                    Assert.AreEqual("my password", record.Properties.Single(p => p.Name == "Password").Value);
                    Assert.AreEqual("test", record.Keywords.Single(p => p.Name == "test").Name);
                    dataContextService.Delete();
                }
            }
        }
    }
}
